/*
 * @Description: http 封装
 * @Author: LiuYan
 * @Date: 2021-12-05 13:54:34
 * @LastEditTime: 2022-03-24 13:34:34
 * @LastEditors: LiuYan
 * @Reference:
 */
import store from '@/store'
const fetch = ({
  url,
  method = 'GET',
  params = {},
  data = {},
  header = {
    'content-type': 'application/json',
  },
  token = true,
  ...other
}) => {
  const options = {
    url,
    method,
    data,
    params,
    header,
    ...other,
  }
  if (token) {
    options.header['Authorization'] = store.state.token
  }
  return new Promise((resolve, reject) => {
    uni.request({
      ...options,
      success(res) {
		  const _res = res.data
		  if (_res.code === 401) {
		    uni.redirectTo({
		      url: '/pages/login/index',
		    })
		  }
		  if (_res.code === 500) {
		    console.error('500')
		  }
        resolve(_res)
      },
      fail(error) {
        reject(error)
      },
    })
  })
}
export default fetch
